/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MagnetoProject;


public class Buffer {

    public static final int SIZE = 30;
    private static int in = 0, out = 0;
    private static int count = 0;
    private static Object[] buffer = new Object[SIZE];
    private static boolean hasData = false;

    public Buffer(){
        count = 0;
        in = 0;
        out = 0;
        buffer = new Object[SIZE];
    }

    public static void enter(Object input){
        while(count == SIZE);

        buffer[in] = input;
        incCount();
        in = (in + 1)%SIZE;
        hasData = true;
    }

    private static synchronized void incCount(){
        count++;
    }

    public static Object remove(){

        //while(count == 0);
        if(count == 0){
            int[] empty = {0,0,0};
            hasData = false;
            return empty;
        }
        
        Object item;
        item = buffer[out];
        decCount();
        out = (out + 1)%SIZE;
        if(count == 0)
            hasData = false;

        return item;
    }

    private static synchronized void decCount(){
        count--;
    }

    public synchronized static boolean haveData(){

        return hasData;
    }



}
